/*
 * librolgps, part of the RolGPS Proyect
 *
 *   
 * Copyright (C) Pablo Alejandro Costesich <rhfixer@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
package deployers.sqlite;

import java.util.Vector;

/**
 *
 * @author xeph
 */
public class Field {

    public static final String AUTOINCREMENT = "AUTOINCREMENT";
    public static final String INTEGER = "INTEGER";
    public static final String PRIMARYKEY = "PRIMARY KEY";
    public static final String IDFIELD = INTEGER + " " + PRIMARYKEY + " " + AUTOINCREMENT;
    public static final String STRING = "VARCHAR";
    private String tableName = null;
    private Vector<String> types = new Vector();
    private boolean hasDataType = false;
    private boolean isPrimaryOrforeignKey = false;

    public Field(String nombre, String... listTypes) {
        this.tableName = nombre;
        for (String type : listTypes) {
            this.addType(type);
        }
    }

    public Field(String nombre) {
        this.tableName = nombre.trim();
    }

    public String getName() {
        return tableName;
    }

    public String getTypesAsString() {
        //ordenar parametros
        String result = "";
        for (Object type : types) {
            result += " " + type;
        }
        return result;
    }

    public void addType(String type) {
        //verificar que no exista un tipo duplicado

        //si es un entero o un string y NO tiene un tipo de dato, anyadir
        if ((type.equals(INTEGER) || type.equals(STRING)) && !this.hasDataType) {
            this.types.add(type);
            this.hasDataType = true;
        } else if ((type.equals(PRIMARYKEY)) && !this.isPrimaryOrforeignKey) {
            this.types.add(type);
            this.isPrimaryOrforeignKey = true;
        } else if (type.equals(AUTOINCREMENT)) {
            boolean hasInteger = false;
            boolean hasPrimaryKey = false;
            for (String eachType : this.types) {
                if (eachType.equals(PRIMARYKEY)) {
                    hasPrimaryKey = true;
                } else if (eachType.equals(INTEGER)) {
                    hasInteger = true;
                }
            }
            if(hasInteger && hasPrimaryKey){
                this.types.add(type);
            }
        }
        if(type.equals(Field.IDFIELD)){
            this.types.clear();
            this.types.add(type);
        }
    }
}
